<!DOCTYPE html>
<html lang="zh-CN">
  <head>
  <meta charset="UTF-8">
  <meta 
    name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta 
    http-equiv="X-UA-Compatible" 
    content="ie=edge">
  <meta 
    name="theme-color" 
    content="#fff" 
    id="theme-color">
  <meta 
    name="description" 
    content="Gas">
  <link 
    rel="icon" 
    href="/img/Kaze.png">
  <title>EF Core 软删除</title>
  
    
      <meta 
        property="og:title" 
        content="EF Core 软删除">
    
    
      <meta 
        property="og:url" 
        content="http://example.com/2020/09/15/EFCore%E8%BD%AF%E5%88%A0%E9%99%A4/index.html">
    
    
      <meta 
        property="og:img" 
        content="/img/Kaze.png">
    
    
    
      <meta 
        property="og:type" 
        content="article">
      <meta 
        property="og:article:published_time" 
        content="2020-09-15">
      <meta 
        property="og:article:modified_time" 
        content="2021-04-08">
      <meta 
        property="og:article:author" 
        content="Gas">
      
        
          <meta 
            property="og:article:tag" 
            content="EFCore">
        
      
    
  
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
    function loadCSS(href, data, attr) {
      var sheet = document.createElement('link');
      sheet.ref = 'stylesheet';
      sheet.href = href;
      sheet.dataset[data] = attr;
      document.head.appendChild(sheet);
    }
    function changeCSS(cssFile, data, attr) {
      var oldlink = document.querySelector(data);
      var newlink = document.createElement("link");
      newlink.setAttribute("rel", "stylesheet");
      newlink.setAttribute("href", cssFile);
      newlink.dataset.prism = attr;
      document.head.replaceChild(newlink, oldlink);
    }
  </script>
  
    
      
      
      
      
        
        
        
        <script>
          function prismThemeChange() {
            if(document.getElementById('theme-color').dataset.mode === 'dark') {
              if(document.querySelector('[data-prism]')) {
                changeCSS('/js/lib/prism/prism-tomorrow.min.css', '[data-prism]', 'prism-tomorrow');
              } else {
                loadCSS('/js/lib/prism/prism-tomorrow.min.css', 'prism', 'prism-tomorrow');
              }
            } else {
              if(document.querySelector('[data-prism]')) {
                changeCSS('/js/lib/prism/prism-okaidia.min.css', '[data-prism]', 'prism-okaidia');
              } else {
                loadCSS('/js/lib/prism/prism-okaidia.min.css', 'prism', 'prism-okaidia');
              }
            }
          }
          prismThemeChange()
        </script>
      
      
        
        <link rel="stylesheet" href="/js/lib/prism/prism-line-numbers.min.css">
      
    
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
  </script>
  
    <script>
      var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
        prismThemeChange();
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
        prismThemeChange();
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
        document.getElementById('theme-color').dataset.mode = getCssMediaQuery();
        prismThemeChange();
      }
    };
    setDarkmode();
    </script>
  
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  
  
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
    <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
  
  <link rel="stylesheet" href="/css/main.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">
  <link rel="stylesheet" href="//at.alicdn.com/t/font_2818401_duebq0kmk47.css">
  
    <link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">
  
<meta name="generator" content="Hexo 5.4.0"></head>

  <body>
    <div class="wrapper">
       
      <nav class="navbar">
  <div class="navbar-logo">
    <span class="navbar-logo-main">
      
        <img 
          class="navbar-logo-img" 
          src="/img/Kaze.png" 
          alt="blog logo">
      
      <span class="navbar-logo-dsc">Gas</span>
    </span>
  </div>
  <div class="navbar-menu">
    
      <a 
        href="/" 
        class="navbar-menu-item">
        
          首页
        
      </a>
    
      <a 
        href="/archives" 
        class="navbar-menu-item">
        
          归档
        
      </a>
    
      <a 
        href="/tags" 
        class="navbar-menu-item">
        
          标签
        
      </a>
    
      <a 
        href="/categories" 
        class="navbar-menu-item">
        
          分类
        
      </a>
    
      <a 
        href="/about" 
        class="navbar-menu-item">
        
          关于
        
      </a>
    
      <a 
        href="/links" 
        class="navbar-menu-item">
        
          友链
        
      </a>
    
    <a 
      class="navbar-menu-item darknavbar" 
      id="dark">
      <i class="iconfont icon-weather"></i>
    </a>
    <a 
      class="navbar-menu-item searchnavbar" 
      id="search">
      <i 
        class="iconfont icon-search" 
        style="font-size: 1.2rem; font-weight: 400;">
      </i>
    </a>
  </div>
</nav> 
      
      <div 
        id="local-search" 
        style="display: none">
        <input
          class="navbar-menu-item"
          id="search-input"
          placeholder="请输入搜索内容..." />
        <div id="search-content"></div>
      </div>
      
      <div class="section-wrap">
        <div class="container">
          <div class="columns">
            <main class="main-column">
<article class="card card-content">
  <header>
    <h1 class="post-title">
      EF Core 软删除
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2020-09-15T07:39:47.000Z">
      <i 
        class="iconfont icon-calendar" 
        style="margin-right: 2px;">
      </i>
      <span>2020-09-15</span>
    </time>
    
      <span class="dot"></span>
      
        <a 
          href="/categories/DotNetCore/" 
          class="post-meta-link">
          DotNetCore
        </a>
      
    
    
      <span class="dot"></span>
      <span>1.6k 字</span>
    
  </div>
  
    <div 
      class="post-meta post-show-meta" 
      style="margin-top: -10px;">
      <div style="display: flex; align-items: center;">
        <i 
          class="iconfont icon-biaoqian" 
          style="margin-right: 2px; font-size: 1.15rem;">
        </i>
        
          
          <a 
            href="/tags/EFCore/" 
            class="post-meta-link">
            EFCore
          </a>
        
      </div>
    </div>
  
  </header>
  <div 
    id="section" 
    class="post-content">
    <h1 id="EF-CORE-软删除"><a href="#EF-CORE-软删除" class="headerlink" title="EF CORE 软删除"></a>EF CORE 软删除</h1><p>向应用添加软删除有如下步骤：</p>
<ol>
<li><p>向需要软删除的实体类添加软删除属性</p>
</li>
<li><p>向 DbContext 中添加代码，以对这些实体类应用查询过滤器</p>
</li>
<li><p>如何设置/重置软删除</p>
</li>
</ol>
<h4 id="1-添加软删除属性"><a href="#1-添加软删除属性" class="headerlink" title="1.添加软删除属性"></a>1.添加软删除属性</h4><p>对于标准的软删除实现，你需要一个布尔标志来控制软删除，例如，这里有一个名叫 SoftDeleted 属性的 Book 实体。</p>
<pre><code class="c#">public class Book : ISoftDelete
&#123;
    public int BookId &#123; get; set; &#125;
    public string Title &#123; get; set; &#125;
    //... 其它无关属性

    public bool SoftDeleted &#123; get; set; &#125;
&#125;
</code></pre>
<p>你可以通过它的名字<code>SoftDeleted</code>来区分软删除属性，如果它的值是<code>true</code>则该实体删除了，这意味着当你创建一个新实体时，它不会被软删除。</p>
<p>我还添加了一个 Book 类的<code>ISoftDelete</code>接口（第 1 行），这个接口表示该类必须有一个可以读写的公共<code>SoftDeleted</code>属性。这个接口将使得在<code>DbContext</code>中配置软删除查询过滤器变得更加容易。</p>
<h4 id="2-配置查询过滤器"><a href="#2-配置查询过滤器" class="headerlink" title="2.配置查询过滤器"></a>2.配置查询过滤器</h4><p>你必须告诉 EF Core 哪个实体类需要一个查询过滤器，该过滤器是查询表达式，用来把不需要被看到的实体过滤掉。你可以在 DbContext 中使用以下代码手动完成此操作。</p>
<pre><code class="c#">public class EfCoreContext : DbContext
&#123;
    public EfCoreContext(DbContextOptions&lt;EfCoreContext&gt; option)
        : base(options)
    &#123;&#125;

    protected override OnModelCreating(ModelBuilder modelBuilder)
    &#123;
        // 省略其它和软删除无关的代码

        modelBuilder.Entity&lt;Book&gt;().HasQueryFilter(p =&gt; !p.SoftDeleted);
    &#125;
&#125;
</code></pre>
<p>这很好，但是让我向你展示一种自动添加查询过滤器的方法。</p>
<p>在 DbContext 中的 <code>OnModelCreating</code> 方法中，你可以通过 Fluent API 配置 EF Core。但是也有一种方法可以判断每个实体类并决定如何配置它。在下面的代码中，你可以看到 foreach 循环依次遍历每个实体类，检查实体类是否实现了<code> ISoftDelete</code> 接口，如果实现了，它将调用我创建的扩展方法来应用正确的软删除过滤器配置。</p>
<pre><code class="c#">protected override void OnModelCreating(ModelBuilder modelBuilder)
&#123;
    // 省略其它无关的代码

    foreach (var entityType in modelBuilder.Model.GetEntityTypes())
    &#123;
        // 省略其它无关的代码

        if (typeof(ISoftDelete).IsAssignableFrom(entityType.ClrType))
        &#123;
            entityType.AddSoftDeleteQueryFilter();
        &#125;
    &#125;
&#125;
</code></pre>
<p>有许多配置可以直接应用于 GetEntityTypes 方法返回的类型，但是设置查询过滤器需要更多的工作。这是因为查询过滤器中的 LINQ 查询需要实体类的类型来创建正确的 LINQ 表达式。为此，我创建了一个小型扩展类，它可以动态创建正确的 LINQ 表达式来配置查询过滤器。</p>
<pre><code class="c#">public static class SoftDeleteQueryExtension
&#123;
    public static void AddSoftDeleteQueryFilter(
        this IMutableEntityType entityData)
    &#123;
        var methodToCall = typeof(SoftDeleteQueryExtension)
            .GetMethod(nameof(GetSoftDeleteFilter),
                BindingFlags.NonPublic | BindingFlags.Static)
            .MakeGenericMethod(entityData.ClrType);
        var filter = methodToCall.Invoke(null, new object[] &#123; &#125;);
        entityData.SetQueryFilter((LambdaExpression)filter);
    &#125;

    private static LambdaExpression GetSoftDeleteFilter&lt;TEntity&gt;()
        where TEntity : class, ISoftDelete
    &#123;
        Expression&lt;Func&lt;TEntity, bool&gt;&gt; filter = x =&gt; !x.SoftDeleted;
        return filter;
    &#125;
&#125;
</code></pre>
<p>我真的很喜欢这个操作，因为它可以节省我的时间，也避免我忘记配置每一个查询过滤器。</p>
<h4 id="3-如何设置-重置软删除"><a href="#3-如何设置-重置软删除" class="headerlink" title="3.如何设置/重置软删除"></a>3.如何设置/重置软删除</h4><p>将“软删除”属性设置为 true 很容易，对应的场景是用户选择一个条目并单击(软)“删除”，这会返回实体的主键。用代码实现如下：</p>
<pre><code class="c#">var entity = context.Books.Single(x =&gt; x.BookId == id);
entity.SoftDeleted = true;
context.SaveChanges();
</code></pre>
<p>重置软删除属性在实际的业务场景中有点复杂。首先，你很可能想要向用户显示一个已删除实体的列表——把它想象成显示某个实体类类型的实例回收站，例如 Book。要做到这一点，需要在你的查询中使用<code>IgnoreQueryFilters</code>方法，这意味着你将得到所有的实体（包括那些没有被软删除的和被软删除的），然后再根据需要选出那些<code> SoftDeleted</code> 属性为 true 的。</p>
<pre><code class="c#">var softDelEntities = _context.Books.IgnoreQueryFilters()
    .Where(x =&gt; x.SoftDeleted).ToList();
</code></pre>
<p>相应的，当你收到一个重设 <code>SoftDeleted</code> 属性的请求时（它通常包含实体类的主键），则要加载此条目时，需要在查询中使用<code>IgnoreQueryFilters</code>方法。</p>
<pre><code class="c#">var entity = context.Books.IgnoreQueryFilters()
     .Single(x =&gt; x.BookId == id);
entity.SoftDeleted = false;
context.SaveChanges();
</code></pre>
<h2 id="使用软删除注意事项"><a href="#使用软删除注意事项" class="headerlink" title="使用软删除注意事项"></a>使用软删除注意事项</h2><p>首先，需要说的是查询过滤器是非常安全的。我的意思是，如果查询过滤器返回 false，那么特定的实体/行将不会包含在查询（包括 Find 和 Include 等）返回的结果集中。你可以使用直接 SQL 绕过它，但除此之外，EF Core 会隐藏你软删除的数据。</p>
<p>但有几点你需要注意。</p>
<h5 id="小心软删除过滤器与其它过滤器的混合使用"><a href="#小心软删除过滤器与其它过滤器的混合使用" class="headerlink" title="小心软删除过滤器与其它过滤器的混合使用"></a>小心软删除过滤器与其它过滤器的混合使用</h5><p>查询过滤器非常适合于软删除，但是查询过滤器更适合于控制对数据组的访问。例如，假设您想要构建一个 Web 应用程序来为多个公司提供服务，比如工资单。在这种情况下，你需要确保 A 公司看不到 B 公司的数据，反之亦然。这种类型的系统称为多租户应用程序，而查询过滤器非常适合此类场景。</p>
<p>问题是，每个实体类型只允许使用一个查询过滤器，因此，如果您想在多租户系统中使用软删除，那么您必须将这两个部分结合起来形成查询过滤器——下面是查询过滤器的示例：</p>
<pre><code class="c#">modelBuilder.Entity&lt;MyEntity&gt;()
    .HasQueryFilter(x =&gt; !x.SoftDeleted
      &amp;&amp; x.TenantId == currentTenantId);
</code></pre>
<p>这看上去很好，但是当你使用<code>IgnoreQueryFilters</code>方法忽略软删除标记进行查询时，它会忽略整个查询过滤器，包括多租户部分。因此，如果不小心，还会显示多租户数据！</p>
<p>答案是为自己构建一个特定于应用程序的<code>IgnoreSoftDeleteFilter</code>方法，如下所示：</p>
<pre><code class="c#">public static IQueryable&lt;TEntity&gt; IgnoreSoftDeleteFilter&lt;TEntity&gt;(
    this IQueryable&lt;TEntity&gt; baseQuery, string currentTenantId)
    where TEntity : class, ITenantId
&#123;
    return baseQuery.IgnoreQueryFilters()
        .Where(x =&gt; x.TenantId == currentTenantId)
&#125;
</code></pre>
<p>这将忽略所有筛选器，然后把多租户筛选器添加回去。这将使它更容易更安全地处理显示/重置被软删除的实体。</p>
<p>更多的看这里吧:<a target="_blank" rel="noopener" href="https://www.cnblogs.com/willick/p/13358580.html">连接</a></p>

  </div>
  <div>
    
      <div 
        class="post-note note-warning copyright" 
        style="margin-top: 42px">
        <p>
          <span style="font-weight: bold;">作者：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="/about">
            Gas
          </a>
        </p>
        <p>
          <span style="font-weight: bold;">文章链接：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="http://example.com/2020/09/15/EFCore%E8%BD%AF%E5%88%A0%E9%99%A4/">
            http://example.com/2020/09/15/EFCore%E8%BD%AF%E5%88%A0%E9%99%A4/
          </a>
        </p>
        <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
      </div>
    
  </div>
</article>
<div class="nav">
  
    <div class="nav-item-prev">
      <a 
        href="/2020/09/15/Docker%E5%AE%89%E8%A3%85RabbitMQ/" 
        class="nav-link">
        <i class="iconfont icon-left nav-prev-icon"></i>
        <div>
          <div class="nav-label">上一篇</div>
          
            <div class="nav-title">Docker安装RabbitMQ </div>
          
        </div>
      </a>
    </div>
  
  
    <div class="nav-item-next">
      <a 
        href="/2020/09/15/Docker%E5%AE%89%E8%A3%85MySql/" 
        class="nav-link">
        <div>
          <div class="nav-label">下一篇</div>
          
            <div class="nav-title">Docker安装MySql </div>
          
        </div>
        <i class="iconfont icon-right nav-next-icon"></i>
      </a>
    </div>
  
</div>

<div 
  class="card card-content toc-card" 
  id="mobiletoc">
  <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#EF-CORE-%E8%BD%AF%E5%88%A0%E9%99%A4"><span class="toc-number">1.</span> <span class="toc-text">EF CORE 软删除</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-%E6%B7%BB%E5%8A%A0%E8%BD%AF%E5%88%A0%E9%99%A4%E5%B1%9E%E6%80%A7"><span class="toc-number">1.0.0.1.</span> <span class="toc-text">1.添加软删除属性</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E9%85%8D%E7%BD%AE%E6%9F%A5%E8%AF%A2%E8%BF%87%E6%BB%A4%E5%99%A8"><span class="toc-number">1.0.0.2.</span> <span class="toc-text">2.配置查询过滤器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-%E5%A6%82%E4%BD%95%E8%AE%BE%E7%BD%AE-%E9%87%8D%E7%BD%AE%E8%BD%AF%E5%88%A0%E9%99%A4"><span class="toc-number">1.0.0.3.</span> <span class="toc-text">3.如何设置&#x2F;重置软删除</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E8%BD%AF%E5%88%A0%E9%99%A4%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9"><span class="toc-number">1.1.</span> <span class="toc-text">使用软删除注意事项</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%B0%8F%E5%BF%83%E8%BD%AF%E5%88%A0%E9%99%A4%E8%BF%87%E6%BB%A4%E5%99%A8%E4%B8%8E%E5%85%B6%E5%AE%83%E8%BF%87%E6%BB%A4%E5%99%A8%E7%9A%84%E6%B7%B7%E5%90%88%E4%BD%BF%E7%94%A8"><span class="toc-number">1.1.0.0.1.</span> <span class="toc-text">小心软删除过滤器与其它过滤器的混合使用</span></a></li></ol></li></ol></li></ol></li></ol></li></ol>
</div></main>
            <aside class="left-column">
              
              <div class="card card-author">
                
  <img 
    src="/img/Kaze.png" 
    class="author-img" 
    alt="author avatar">

<p class="author-name">Gas</p>
<p class="author-description">想当画家的咸鱼罢了</p>
<div class="author-message">
  <a 
    class="author-posts-count" 
    href="/archives">
    <span>51</span>
    <span>文章</span>
  </a>
  <a 
    class="author-categories-count" 
    href="/categories">
    <span>16</span>
    <span>分类</span>
  </a>
  <a 
    class="author-tags-count" 
    href="/tags">
    <span>44</span>
    <span>标签</span>
  </a>
</div>

  <div class="author-card-society">
    
      <div class="author-card-society-icon">
        <a target="_blank" rel="noopener" href="https://github.com/Uranus-s">
          <i class="iconfont social_github icon-github society-icon"></i>
        </a>
      </div>
    
      <div class="author-card-society-icon">
        <a target="_blank" rel="noopener" href="https://gitee.com/L-sama">
          <i class="iconfont social_github icon-gitee2 society-icon"></i>
        </a>
      </div>
    
  </div>

              </div>
               <div class="sticky-tablet">
  
  
    <article class="display-when-two-columns spacer">
      <div class="card card-content toc-card">
        <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#EF-CORE-%E8%BD%AF%E5%88%A0%E9%99%A4"><span class="toc-number">1.</span> <span class="toc-text">EF CORE 软删除</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-%E6%B7%BB%E5%8A%A0%E8%BD%AF%E5%88%A0%E9%99%A4%E5%B1%9E%E6%80%A7"><span class="toc-number">1.0.0.1.</span> <span class="toc-text">1.添加软删除属性</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E9%85%8D%E7%BD%AE%E6%9F%A5%E8%AF%A2%E8%BF%87%E6%BB%A4%E5%99%A8"><span class="toc-number">1.0.0.2.</span> <span class="toc-text">2.配置查询过滤器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-%E5%A6%82%E4%BD%95%E8%AE%BE%E7%BD%AE-%E9%87%8D%E7%BD%AE%E8%BD%AF%E5%88%A0%E9%99%A4"><span class="toc-number">1.0.0.3.</span> <span class="toc-text">3.如何设置&#x2F;重置软删除</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E8%BD%AF%E5%88%A0%E9%99%A4%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9"><span class="toc-number">1.1.</span> <span class="toc-text">使用软删除注意事项</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%B0%8F%E5%BF%83%E8%BD%AF%E5%88%A0%E9%99%A4%E8%BF%87%E6%BB%A4%E5%99%A8%E4%B8%8E%E5%85%B6%E5%AE%83%E8%BF%87%E6%BB%A4%E5%99%A8%E7%9A%84%E6%B7%B7%E5%90%88%E4%BD%BF%E7%94%A8"><span class="toc-number">1.1.0.0.1.</span> <span class="toc-text">小心软删除过滤器与其它过滤器的混合使用</span></a></li></ol></li></ol></li></ol></li></ol></li></ol>
      </div>
    </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header">
    <i 
      class="iconfont icon-fenlei" 
      style="padding-right: 2px;">
    </i>分类
  </div>
  <div class="categories-list">
    
      <a href="/categories/Java/">
        <div class="categories-list-item">
          Java
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/SpringBoot/">
        <div class="categories-list-item">
          SpringBoot
          <span class="categories-list-item-badge">16</span>
        </div>
      </a>
    
      <a href="/categories/WPF/">
        <div class="categories-list-item">
          WPF
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
      <a href="/categories/Docker/">
        <div class="categories-list-item">
          Docker
          <span class="categories-list-item-badge">3</span>
        </div>
      </a>
    
      <a href="/categories/Net/">
        <div class="categories-list-item">
          Net
          <span class="categories-list-item-badge">2</span>
        </div>
      </a>
    
      <a href="/categories/DotNetCore/">
        <div class="categories-list-item">
          DotNetCore
          <span class="categories-list-item-badge">2</span>
        </div>
      </a>
    
      <a href="/categories/%E6%8E%92%E5%BA%8F/">
        <div class="categories-list-item">
          排序
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
      <a href="/categories/Redis/">
        <div class="categories-list-item">
          Redis
          <span class="categories-list-item-badge">6</span>
        </div>
      </a>
    
      <a href="/categories/COS/">
        <div class="categories-list-item">
          COS
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
      <a href="/categories/Spring/">
        <div class="categories-list-item">
          Spring
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
      <a href="/categories/IText/">
        <div class="categories-list-item">
          IText
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
      <a href="/categories/GateWay/">
        <div class="categories-list-item">
          GateWay
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
      <a href="/categories/Jenkins/">
        <div class="categories-list-item">
          Jenkins
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
      <a href="/categories/Netty/">
        <div class="categories-list-item">
          Netty
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
      <a href="/categories/Log4j/">
        <div class="categories-list-item">
          Log4j
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
      <a href="/categories/algorithm/">
        <div class="categories-list-item">
          algorithm
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header">
    <i 
      class="iconfont icon-biaoqian" 
      style="padding-right: 2px;">
    </i>热门标签
  </div>
  <div class="tags-list">
    
      <a 
        href="/tags/SpringBoot/" 
        title="SpringBoot">
        <div class="tags-list-item">SpringBoot</div>
      </a>
    
      <a 
        href="/tags/Spring/" 
        title="Spring">
        <div class="tags-list-item">Spring</div>
      </a>
    
      <a 
        href="/tags/Java/" 
        title="Java">
        <div class="tags-list-item">Java</div>
      </a>
    
      <a 
        href="/tags/Redis/" 
        title="Redis">
        <div class="tags-list-item">Redis</div>
      </a>
    
      <a 
        href="/tags/Docker/" 
        title="Docker">
        <div class="tags-list-item">Docker</div>
      </a>
    
      <a 
        href="/tags/DotNetCore/" 
        title="DotNetCore">
        <div class="tags-list-item">DotNetCore</div>
      </a>
    
      <a 
        href="/tags/IDEA/" 
        title="IDEA">
        <div class="tags-list-item">IDEA</div>
      </a>
    
      <a 
        href="/tags/Maven/" 
        title="Maven">
        <div class="tags-list-item">Maven</div>
      </a>
    
      <a 
        href="/tags/RabbitMQ/" 
        title="RabbitMQ">
        <div class="tags-list-item">RabbitMQ</div>
      </a>
    
      <a 
        href="/tags/Jackson/" 
        title="Jackson">
        <div class="tags-list-item">Jackson</div>
      </a>
    
      <a 
        href="/tags/snowflake/" 
        title="snowflake">
        <div class="tags-list-item">snowflake</div>
      </a>
    
      <a 
        href="/tags/algorithm/" 
        title="algorithm">
        <div class="tags-list-item">algorithm</div>
      </a>
    
      <a 
        href="/tags/Log4j/" 
        title="Log4j">
        <div class="tags-list-item">Log4j</div>
      </a>
    
      <a 
        href="/tags/Socket/" 
        title="Socket">
        <div class="tags-list-item">Socket</div>
      </a>
    
      <a 
        href="/tags/Netty/" 
        title="Netty">
        <div class="tags-list-item">Netty</div>
      </a>
    
      <a 
        href="/tags/Jenkins/" 
        title="Jenkins">
        <div class="tags-list-item">Jenkins</div>
      </a>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
            <aside class="right-column">
              <div class="sticky-widescreen">
  
  
    <article class="card card-content toc-card">
      <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#EF-CORE-%E8%BD%AF%E5%88%A0%E9%99%A4"><span class="toc-number">1.</span> <span class="toc-text">EF CORE 软删除</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-%E6%B7%BB%E5%8A%A0%E8%BD%AF%E5%88%A0%E9%99%A4%E5%B1%9E%E6%80%A7"><span class="toc-number">1.0.0.1.</span> <span class="toc-text">1.添加软删除属性</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E9%85%8D%E7%BD%AE%E6%9F%A5%E8%AF%A2%E8%BF%87%E6%BB%A4%E5%99%A8"><span class="toc-number">1.0.0.2.</span> <span class="toc-text">2.配置查询过滤器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-%E5%A6%82%E4%BD%95%E8%AE%BE%E7%BD%AE-%E9%87%8D%E7%BD%AE%E8%BD%AF%E5%88%A0%E9%99%A4"><span class="toc-number">1.0.0.3.</span> <span class="toc-text">3.如何设置&#x2F;重置软删除</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E8%BD%AF%E5%88%A0%E9%99%A4%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9"><span class="toc-number">1.1.</span> <span class="toc-text">使用软删除注意事项</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%B0%8F%E5%BF%83%E8%BD%AF%E5%88%A0%E9%99%A4%E8%BF%87%E6%BB%A4%E5%99%A8%E4%B8%8E%E5%85%B6%E5%AE%83%E8%BF%87%E6%BB%A4%E5%99%A8%E7%9A%84%E6%B7%B7%E5%90%88%E4%BD%BF%E7%94%A8"><span class="toc-number">1.1.0.0.1.</span> <span class="toc-text">小心软删除过滤器与其它过滤器的混合使用</span></a></li></ol></li></ol></li></ol></li></ol></li></ol>
    </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header">
    <i 
      class="iconfont icon-wenzhang_huaban" 
      style="padding-right: 2px;">
    </i>最近文章
  </div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2022-07-08</div>
        <a href="/2022/07/08/SpringBoot-RabbitMQ%E5%BA%94%E7%94%A8/"><div class="recent-posts-item-content">SpringBoot-RabbitMQ应用</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2022-03-26</div>
        <a href="/2022/03/26/IDEA%E9%83%A8%E7%BD%B2Docker%E9%A0%85%E7%9B%AE/"><div class="recent-posts-item-content">IDEA部署Docker項目</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2022-03-04</div>
        <a href="/2022/03/04/%E5%9F%BA%E4%BA%8EJackson%E7%9A%84JsonFilter%E5%AE%9E%E7%8E%B0%E5%8A%A8%E6%80%81%E6%95%B0%E6%8D%AE%E5%A1%AB%E5%86%99/"><div class="recent-posts-item-content">基于Jackson的JsonFilter实现动态数据填写</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2022-02-16</div>
        <a href="/2022/02/16/SpringBoot-%E8%87%AA%E5%AE%9A%E4%B9%89%E6%B3%A8%E8%A7%A3%E5%8D%95%E5%8F%82%E6%95%B0Post/"><div class="recent-posts-item-content">SpringBoot-自定义注解单参数Post</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
          </div>
        </div>
      </div>
    </div>
     
    <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>
          Copyright ©
          
            2020 -
          
          2022
        </span>
        &nbsp;
        <a 
          href="/" 
          class="footer-link">
          Gas
        </a>
      </div>
    </div>

    
      <div class="footer-dsc">
        
          Powered by
          <a 
            href="https://hexo.io/" 
            class="footer-link" 
            target="_blank" 
            rel="nofollow noopener noreferrer">
            &nbsp;Hexo
          </a>
        
        
          <span>&nbsp;|&nbsp;</span>
        
        
          Theme -
          <a 
            href="https://github.com/theme-kaze" 
            class="footer-link" 
            target="_blank"
            rel="nofollow noopener noreferrer">
            &nbsp;Kaze
          </a>
        
      </div>
    
    
    
    
      <div class="footer-dsc">
        
          本站总访问量<span id="busuanzi_value_site_pv"></span>次
        
        
          <span>&nbsp;|&nbsp;</span>
        
        
          本站总访客数<span id="busuanzi_value_site_uv"></span>次
        
      </div>
      
    
</footer> 
    
  <a 
    role="button" 
    id="scrollbutton" 
    class="basebutton" 
    aria-label="回到顶部">
    <i class="iconfont icon-arrowleft button-icon"></i>
  </a>

<a 
  role="button" 
  id="menubutton" 
  class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a 
  role="button" 
  id="popbutton" 
  class="basebutton" 
  aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a 
  role="button" 
  id="darkbutton" 
  class="basebutton darkwidget" 
  aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a 
  role="button" 
  id="searchbutton" 
  class="basebutton searchwidget" 
  aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a> 
     
     
     
      <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img')
    var i
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a')
      wrapper.setAttribute('href', img[i].getAttribute('data-src'))
      wrapper.setAttribute('aria-label', 'illustration')
      wrapper.style.cssText =
        'width: 100%; display: flex; justify-content: center;'
      if (img[i].alt) wrapper.dataset.caption = img[i].alt
      wrapper.dataset.nolink = true
      img[i].before(wrapper)
      wrapper.append(img[i])
      var divWrap = document.createElement('div')
      divWrap.classList.add('gallery')
      wrapper.before(divWrap)
      divWrap.append(wrapper)
    }
    baguetteBox.run('.gallery')
  }
</script>
<script>
  loadScript(
    "/js/lib/lightbox/baguetteBox.min.js",
    addImgLayout
  )
</script>
 
     
     
    <script src="/js/main.js"></script> 
    
      <script> 
        loadScript('/js/lib/busuanzi.min.js') 
      </script>
     
    
      <script>
        var addLazyload = function () {
          var observer = lozad('.lozad', {
            load: function (el) {
              el.srcset = el.getAttribute('data-src')
            },
            loaded: function (el) {
              el.classList.add('loaded')
            },
          })
          observer.observe()
        }
      </script>
      <script>
        loadScript('/js/lib/lozad.min.js', addLazyload)
      </script>
     
    
    
      <script>
        setTimeout(() => {localSearch("search.json")}, 0)
      </script>
    
  </body>
</html>
